package com.heima.article.service.impl;

import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.StoreCountService;
import com.heima.common.exception.CustException;
import com.heima.feigns.WemediaFeign;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.ArticleVisitStreamMess;
import com.heima.model.statistic.pojos.WmNewsStatistics;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

@Service
@Slf4j
public class StoreCountServiceImpl implements StoreCountService {

    @Autowired
    ApArticleMapper apArticleMapper;

    @Autowired
    WemediaFeign wemediaFeign;

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void StoreCount(ArticleVisitStreamMess mess) {

        log.info("开始调用");
        //根据文章id查询wm_user的user_id
        ResponseResult<Integer> result = wemediaFeign.findUserId(mess.getArticleId());
        if(!result.checkCode()){
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR,"远程调用失败");
        }
        Integer userId = result.getData();
        log.info("用户id是 {}",userId);
        //查询出是否有当天读者的数据分析
        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" );
        Date d1= new Date();
        String str1 = sdf1.format(d1);
        Query query = Query.query(Criteria.where("userId").is(userId).and("creatTime").is(str1));
        WmNewsStatistics wmNewsStatistics = mongoTemplate.findOne(query, WmNewsStatistics.class);
        if(wmNewsStatistics == null){
            WmNewsStatistics newsStatistics = new WmNewsStatistics();

            newsStatistics.setUserId(userId);
            newsStatistics.setComment( mess.getComment());
            newsStatistics.setReadCount( mess.getView());
            newsStatistics.setFanReadNum(mess.getFansViews());
            newsStatistics.setCollect(mess.getCollect());
            newsStatistics.setCreatTime(str1);
            newsStatistics.setArticleCount(0l);
            newsStatistics.setLikes(mess.getLike());
            newsStatistics.setUnLikes(mess.getUnLike());

            mongoTemplate.save(newsStatistics);
        }
        log.info("存储数据");
        wmNewsStatistics.setComment(wmNewsStatistics.getComment()+ mess.getComment());
        wmNewsStatistics.setReadCount(wmNewsStatistics.getReadCount()+ mess.getView());
        wmNewsStatistics.setFanReadNum(wmNewsStatistics.getFanReadNum()+ mess.getFansViews());
        wmNewsStatistics.setCollect(wmNewsStatistics.getCollect()+ mess.getCollect());
        wmNewsStatistics.setLikes(wmNewsStatistics.getLikes()+ mess.getLike());
        wmNewsStatistics.setUnLikes(wmNewsStatistics.getUnLikes()+ mess.getUnLike());
        mongoTemplate.save(wmNewsStatistics);
        log.info("存储数据完毕");
    }
}
